package co.th.helper.common;

import java.math.BigDecimal;

public class CommonUtils {

	public static boolean chkIsnull(String input){
		if (null == input || "".equalsIgnoreCase(input)){
			return true;
		}
		return false;
	}
	
	public static boolean chkIsnull(BigDecimal input){
		if (null == input || input.intValue() <= 0){
			return true;
		}
		return false;
	}
	
	public static String getString(String input){
		if (null == input || "".equalsIgnoreCase(input)){
			return "";
		}
		return input.trim();
	}
	
	public static Object[] convListToArray(java.util.List<Object> input){
		if (input == null || input.isEmpty()){
			return new Object[0];
		}
		Object[] output = new Object[input.size()];
		for(int i=0 ; i<input.size() ; i++){
			output[i] = input.get(i);
		}
		return output;
	}
	
	public static String convBigDecimalToString(BigDecimal input, int length){
		String str = input.intValue()+"";
		if (length > str.length()){
			for(int i=0 ; i<=(length-str.length()) ; i++){
				str = "0" + str;
			}
		}else if (length < str.length()){
			str = str.substring(str.length()-length);
		}
		return str;
	}
	
	public static java.sql.Blob convInputStreamToBlob(java.io.InputStream stream) throws Exception{
		java.io.ByteArrayOutputStream bytes = new java.io.ByteArrayOutputStream();
		java.sql.Blob blob = null;
		org.apache.commons.fileupload.util.Streams.copy(stream, bytes, true);
		blob = new javax.sql.rowset.serial.SerialBlob(bytes.toByteArray());
		return blob;
	}
}
